#include "movingpoint.h"

MovingPoint::MovingPoint()
{
    damping =  .45;
    attraction = .2;
    mass = 1.0;
    bool targeting = false;

    target.setX( 0.0 );
    target.setY( 0.0 );
    value.setX( 0.0 );
    value.setY( 0.0 );
}

//---------------------ADVANCE----------------------
void MovingPoint::advance( )
{
    if(! ( targetingX || targetingY ) )
        return;

    if (targetingX )
        advanceX() ;
    if( targetingY )

        advanceY();

}

void MovingPoint::advanceX()
{
    force.setX( force.x() + attraction * (target.x() - value.x()) );

    if( abs(target.x() - value.x()) < 1 )
    {
        value.setX( target.x() );
        targetingX = false;
    }
    accel.setX( force.x() / mass );
    vel.setX( (vel.x() + accel.x()) * damping );
    value.setX( value.x() + vel.x() );

    if( value.x() < 0 )
        value.setX( 0.0 );

    force.setX( 0.0 );
}

void MovingPoint::advanceY()
{
    force.setY( force.y() + attraction * (target.y() - value.y()) );

    if( abs(target.y() - value.y()) < 1 )
    {
        value.setY( target.y() );
        targetingY = false;
    }
    accel.setY( force.y() / mass );
    vel.setY( (vel.y() + accel.y()) * damping );
    value.setY( value.y() + vel.y() );

    if( value.y() < 0 )
        value.setY( 0.0 );

    force.setY( 0.0 );
}



//---------------------SET VALUE----------------------
void MovingPoint::setValue( QPoint pt )
{
    setValue( pt.x(), pt.y() );
}

void MovingPoint::setValue( QPointF pt )
{
    setValue( pt.x(), pt.y() );
}
void MovingPoint::setValue( float x, float y)
{
    value.setX( x );
    value.setY( y );
    targetingX = false;
    targetingY = false;
}

void MovingPoint::setValueX( float x )
{
    setValue( x, value.y() );
}

void MovingPoint::setValueY( float y )
{
    setValue( value.x(), y );
}

//---------------------SET TARGET----------------------
void MovingPoint::setTarget( QPoint pt )
{
    setTarget( pt.x(), pt.y() );
}

void MovingPoint::setTarget( QPointF pt )
{
    setTarget( pt.x(), pt.y() );
}

void MovingPoint::setTarget( float x, float y)
{
    target.setX( x );
    target.setY( y );
    targetingX = true;
    targetingY = true;
}

void MovingPoint::setTargetX( float x )
{
    setTarget( x, target.y() );
}

void MovingPoint::setTargetY( float y )
{
    setTarget( target.x(), y );
}

QPoint MovingPoint::getValue()
{
    return value;
}

QPoint MovingPoint::getTarget()
{
    return target;
}

//-------------------------DAMPING AND ATTRACTION -------------------
void MovingPoint::setDamping( float d )
{
    damping = d;
}

void MovingPoint::setAttraction( float a )
{
    attraction = a;
}
